package com.csst.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;

import com.csst.bean.Order;
import com.csst.bean.User;
import com.csst.service.AccountService;
import com.csst.service.OrderService;
import com.csst.util.HttpUtil;
import com.csst.util.SignatureTool;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class QuickAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3598909783466904537L;

	@Resource
	private AccountService aservice;
	@Resource
	private OrderService oservice;

	private String bankpassword;
	private String accpassword;
	

	public String getAccpassword() {
		return accpassword;
	}

	public void setAccpassword(String accpassword) {
		this.accpassword = accpassword;
	}

	public String getBankpassword() {
		return bankpassword;
	}

	public void setBankpassword(String bankpassword) {
		this.bankpassword = bankpassword;
	}

	public String alipay() {
		ActionContext context = ActionContext.getContext();
		User user = (User) context.getSession().get("user");
		Order order = (Order) context.getSession().get("order");
		if (accpassword.equals(user.getAccount().getAccpassword())) {
			if (order.getMoney() <= user.getAccount().getMoney()) {
				user.getAccount().setMoney(
						user.getAccount().getMoney() - order.getMoney());
				aservice.updateAccount(user.getAccount());
				order.setState(2);
				order.setPayerId(user.getUserId());
				oservice.updateOrder(order);
				return "success";
			}
			return "fail";
		}
		return "wrongpwd";
	}

	public String cardpay() {
		ActionContext context = ActionContext.getContext();
		User user = (User) context.getSession().get("user");
		Order order = (Order) context.getSession().get("order");
		Map<String, String> map = new HashMap<String, String>();
		map.put("merID", "123456");
		map.put("fromBankCardNum", user.getUserbank().getCardId());
		map.put("toBankCardNum", "6225123498760000123");
		long m = (long) (order.getMoney() * 100);
		map.put("amount", String.valueOf(m));
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateString = formatter.format(currentTime);
		map.put("orderDate", dateString);
		SignatureTool tool = new SignatureTool();
		tool.setTransferParameter(123456, user.getUserbank().getCardId(),
				"6225123498760000123", m, dateString, bankpassword,
				"jksas-jklwj-lwjjh-wmkhs-jklkj");
		String merSignMsg = tool.signature();
		map.put("merSignMsg", merSignMsg);
		String result = HttpUtil.doPost(
				"http://bank:8080/obs/merchant/transfer", map);
		try {
			JSONObject job = new JSONObject(result);
			String money=String.valueOf(m);
			if(job.get("result").equals("IC")&&job.get("amount").equals(money))
			{
				return "wrongpwd";
			}
			else if(job.get("result").equals("AI")&&job.get("amount").equals(money))
			{
				return "fail";
			}
			else if(job.get("result").equals("SC")&&job.get("amount").equals(money))
			{
				order.setState(2);
				order.setPayerId(user.getUserId());
				oservice.updateOrder(order);
				return "success";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
